package sz.szsmk.citizencard.ui.adapter;

import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.ItemWeatherHeadBinding;
import sz.szsmk.citizencard.databinding.WeatherItemBinding;
import sz.szsmk.citizencard.model.network.bean.response.WeathDetailRes;
import sz.szsmk.citizencard.ui.activity.HomeWeathActivity;
import sz.szsmk.citizencard.util.WeathUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/8/10 11:38
 * description:
 */
public class WeathDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private HomeWeathActivity mContext;

    private WeathDetailRes response;
    private List<WeathDetailRes.DailyInfoListBean> viewData;

    public WeathDetailAdapter(HomeWeathActivity homeWeathActivity) {
        mContext = homeWeathActivity;
    }

    @Override
    public int getItemViewType(int position) {
        if (viewData != null && position == 0)
            return R.layout.item_weather_head;
        else
            return R.layout.weather_item;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == R.layout.item_weather_head)
            return WeathHeadHolder.creat(parent);
        else
            return WeathDetailHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0)
            loadHead(((WeathHeadHolder) holder).binding);
        else
            loadItemData(position - 1, ((WeathDetailHolder) holder).binding);
    }

    public void setResponse(WeathDetailRes response) {
        this.response = response;
        this.viewData = response.getDailyInfoList();
    }

    private void loadHead(ItemWeatherHeadBinding dataBinding) {
        if (response == null)
            return;

        dataBinding.weathTime.setText(response.getUpdateTime() + "更新");
        dataBinding.weathWeath.setText(response.getWeather());
        dataBinding.weathWind.setText(response.getWindDirection());
        dataBinding.weathHumidity.setText(response.getHumidity() + "%");
        dataBinding.weathWindlv.setText(response.getWindS());
        dataBinding.weathTemp.setText(response.getTemp() + "°");
        WeathUtils.setWeath(dataBinding.weathWeathIcon, response.getWeather());
        dataBinding.weathWarm.setText(response.getMinTemperature() + "° ~ " + response.getMaxTemperature() + "°");
        dataBinding.weathQuality.setText(response.getAirQuantity());
    }

    private void loadItemData(int position, WeatherItemBinding binding) {
        if (viewData != null) {
            WeathDetailRes.DailyInfoListBean bean = viewData.get(position);
            binding.weathRvData.setText(bean.getDate());
            binding.weathRvWarmStart.setText(bean.getMinTemperature());
            binding.weathRvWarmEnd.setText(bean.getMaxTemperature());
            String weather = bean.getWeather();
            WeathUtils.setWeath(binding.weathRvIcon, weather);
        }
    }

    @Override
    public int getItemCount() {
        return null == viewData ? 0 : viewData.size() + 1;
    }

    public static class WeathDetailHolder extends RecyclerView.ViewHolder {
        private WeatherItemBinding binding;

        public static WeathDetailHolder creat(ViewGroup parent) {
            WeatherItemBinding binding = WeatherItemBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new WeathDetailHolder(binding);
        }

        public WeathDetailHolder(WeatherItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    public static class WeathHeadHolder extends RecyclerView.ViewHolder {
        private ItemWeatherHeadBinding binding;

        public static WeathHeadHolder creat(ViewGroup parent) {
            ItemWeatherHeadBinding binding = ItemWeatherHeadBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new WeathHeadHolder(binding);
        }

        public WeathHeadHolder(ItemWeatherHeadBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
